package com.situ.springboot.controller;

import com.situ.springboot.annotation.MyLog;
import com.situ.springboot.pojo.Admin;
import com.situ.springboot.pojo.query.AdminQuery;
import com.situ.springboot.service.IAdminService;
import com.situ.springboot.util.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin")
//@CrossOrigin //解决跨域问题
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @RequestMapping("/list")
    public Result list(AdminQuery adminQuery) {
        System.out.println("AdminController.list");
        /*Student student1 = new Student(1, "zhangsan1", 23, "男");
        Student student2 = new Student(2, "zhangsan2", 23, "男");
        Student student3 = new Student(3, "zhangsan3", 23, "男");
        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);*/
        List<Admin> list = adminService.list(adminQuery);
        return Result.ok(list);
    }

    @MyLog
    @RequestMapping("/login")
    public Result login(String name, String password, HttpSession session) {
        System.out.println("AdminController.login");
        Admin admin = adminService.login(name, password);
        if (admin == null) {
            return Result.error("用户名或密码错误");
        }
        if (admin.getStatus() == 0) {
            return Result.error("该用户已经被禁用");
        }

        //登录成功
        session.setAttribute("admin", admin);
        return Result.ok("登录成功");
    }

    @MyLog
    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("admin");
        return Result.ok("退出成功");
    }
}
